package com.cqqs.controller;

import com.cqqs.common.bean.BaseRes;
import com.cqqs.controller.dto.req.concat.ConcatPageReq;
import com.cqqs.controller.dto.req.concat.ContactSaveReq;
import com.cqqs.controller.dto.req.concat.ContactUpdateReq;
import com.cqqs.controller.dto.res.concat.ContactRes;
import com.cqqs.page.TableDataInfo;
import com.cqqs.service.TechConcatService;
import com.cqqs.utils.excel.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author mjxie
 * @date 2022/09/23
 */
@RestController
@Api(tags = "科技管理-通讯录管理接口")
@Slf4j
@RequestMapping("/concat")
public class TechConcatController extends BaseController {


    private final TechConcatService techConcatService;

    @Autowired
    public TechConcatController(TechConcatService techConcatService) {
        this.techConcatService = techConcatService;
    }

    @ApiOperation(value = "保存通讯录")
    @PostMapping("")
    public BaseRes save(@Validated @RequestBody ContactSaveReq contactSaveReq) {
        techConcatService.save(contactSaveReq);
        return new BaseRes<>();
    }

    @PutMapping("")
    @ApiOperation(value = "修改通讯录")
    public BaseRes update(@Validated @RequestBody ContactUpdateReq contactUpdateReq) {
        techConcatService.update(contactUpdateReq);
        return new BaseRes<>();
    }

    @ApiOperation(value = "批量删除通讯录")
    @DeleteMapping("")
    public BaseRes delete(@RequestBody Integer[] ids) {
        techConcatService.deleteByBatch(ids);
        return new BaseRes();
    }

    @ApiOperation(value = "获取通讯录详情")
    @GetMapping("/detail/{id}")
    public BaseRes<ContactRes> detail(@PathVariable Integer id) {
        ContactRes contactRes = techConcatService.findById(id);
        return new BaseRes<>(contactRes);
    }


    @ApiOperation(value = "分页查询通讯录")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "页码 如1 2 3"),
            @ApiImplicitParam(name = "pageSize", value = "页码如 10 15 "),
            @ApiImplicitParam(name = "orderByColumn", value = "排序列字段名"),
            @ApiImplicitParam(name = "isAsc", value = "排序方式 升序=ascending/asc,降序=descending/desc ")})
    @GetMapping(value = "/page")
    public BaseRes<TableDataInfo<ContactRes>> page(ConcatPageReq concatPageReq) {
        startPage();
        List<ContactRes> list = techConcatService.findByConcatPageReq(concatPageReq);
        return new BaseRes<>(new TableDataInfo().getDataTable(list));
    }

    @ApiOperation(value = "列表导出")
    @PostMapping("/export")
    public void export(@RequestBody ConcatPageReq concatPageReq, HttpServletResponse response) {
        List<ContactRes> list = techConcatService.findByConcatPageReq(concatPageReq);
        ExcelUtil<ContactRes> util = new ExcelUtil<>(ContactRes.class);
        util.exportExcel(response, list, "通讯录");
    }


}
